package com.octopus.calendar;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CalendarUtils {
	
	public static List<Calendar> getMonthCalendarList(Date date, Locale aLocale){
		List<Calendar> list = new ArrayList<Calendar>();
		if(date == null){
			return list;
		}
		if(aLocale == null){
			aLocale = Locale.getDefault();
		}
		Calendar calendar = Calendar.getInstance(aLocale);
		calendar.setTime(date);
		
		return getMonthCalendarList(calendar, aLocale);
	}
	
	public static List<Calendar> getMonthCalendarList(Calendar calendar, Locale aLocale){
		List<Calendar> list = new ArrayList<Calendar>();
		if(calendar == null){
			return list;
		}
		if(aLocale == null ){
			aLocale = Locale.getDefault();
		}
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH); //January is 0, and so on.
		return getMonthCalendarList(year, month, aLocale);
	}
	
	public static List<Calendar> getMonthCalendarList(int year, int month, Locale aLocale){
		List<Calendar> result = new ArrayList<Calendar>();
		int days[] = monthDays(year, month);
		if(days[0] > 0){
			int realYear = days[1];
			int realMonth = days[2];
			Calendar calendar = Calendar.getInstance(aLocale);
			calendar.set(Calendar.YEAR, realYear);
			calendar.set(Calendar.MONTH, realMonth);
			for(int i = 1; i <= days[0] ; i++){
				calendar.set(Calendar.DAY_OF_MONTH, i);
				Calendar calendarInner = Calendar.getInstance(aLocale);
				calendarInner.setTime(calendar.getTime());
				result.add(calendarInner);
			}
			return result;
		}else{
			return result;
		}
	}
	
	public static List<Calendar> getCurrentMonthCalendarList(Locale aLocale){
		Calendar calendar = Calendar.getInstance(aLocale);
		return getMonthCalendarList(calendar, aLocale);
	}
	
	
	public static int[] monthDays(int year, int month) {
		int result[] = new int[3];
		if (year <= 0 || month < 0) {
			return result;
		} else {
			int yearCount = month / 12;
			int realMonth = month % 12;
			year = year + yearCount;
			month = realMonth;
			result[1] = year;
			result[2] = month;
		}
		switch (month) {
		case 0: {
			result[0] = 31;
			return result;
		}
		case 1: {
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
				result[0] = 29;
				return result;
			} else {
				result[0] = 28;
				return result;
			}
		}
		case 2: {
			result[0] = 31;
			return result;
		}
		case 3: {
			result[0] = 30;
			return result;
		}
		case 4: {
			result[0] = 31;
			return result;
		}
		case 5: {
			result[0] = 30;
			return result;
		}
		case 6: {
			result[0] = 31;
			return result;
		}
		case 7: {
			result[0] = 31;
			return result;
		}
		case 8: {
			result[0] = 30;
			return result;
		}
		case 9: {
			result[0] = 31;
			return result;
		}
		case 10: {
			result[0] = 30;
			return result;
		}
		case 11: {
			result[0] = 31;
			return result;
		}
		default:
			return result;
		}
	}
	
	public static void main(String args[]){
		Calendar calendar = Calendar.getInstance();
		System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
		System.out.println(Calendar.SUNDAY);
	}
}
